<?php

/**
 * 模块：广告管理
 * @copyright (C)2011 Cenwor Inc.
 * @author Moyo <dev@uuland.org>
 * @package module
 * @name ad.mod.php
 * @version 1.0
 */

class ModuleObject extends MasterObject
{
	function ModuleObject( $config )
	{
		$this->MasterObject($config);
		$runCode = Load::moduleCode($this);
		$this->$runCode();
	}
	function Main(){
		$this->vlist();
	}
	function vlist(){	
		$city = logic('misc')->CityList();
		for($i=0;$i<count($city);$i++){
			$newcity[$city[$i]['cityid']]=$city[$i]['cityname'];
		}
		$city_id=$this->Post['city_id']==''?$this->Get['city_id']:$this->Post['city_id'];	
		$ad_list = logic('ad')->GetList($city_id);
		include handler('template')->file('@admin/ad_list');
	}
	function add(){
		$action='?mod=ad&code=doadd';
		$position_list = logic('ad')->GetPositionList();
		$city = logic('misc')->CityList();
		include handler('template')->file('@admin/ad_add');
	}
	function doadd(){
		extract($this->Post);
		if($ad_name == ''|| $position_id == ''||$start_time==''||$end_time==''||$ad_link==''||$ad_code==''){
			$this->Messager("请将参数都填写完整!", -1);
		}
		$start_time = strtotime($start_time);
		$end_time = strtotime($end_time);
		if ($start_time>=$end_time){
			$this->Messager("开始时间不能等于大于结束时间", -1);
		}
		$data=array(
				'ad_name' => $ad_name,
				'ad_link' => $ad_link,
				'ad_code' => $ad_code,
				'position_id'=>$position_id,
				'city_id'=>$city_id,
				'start_time'=>$start_time,
				'end_time'=>$end_time,
				'enabled'=>$enabled,
					
		);
		$aid = logic('ad')->add($data);
		if (!$aid) $this->Messager('添加广告失败！请重试', -1);
		$this->Messager("操作成功",'?mod=ad');
		
	}
	function edit(){
		extract($this->Get);
		$action='?mod=ad&code=doedit';
		$city = logic('misc')->CityList();
		$position_list = logic('ad')->GetPositionList();
		$ad = logic('ad')->getOne($id);
		if(!$ad)$this->Messager('参数有误!请重试', '?mod=ad');
		include handler('template')->file('@admin/ad_edit');
	}
	
	function doedit(){
		extract($this->Post);
		if($id==""||$ad_name == ''|| $position_id == ''||$start_time==''||$end_time==''||$ad_link==''||$ad_code==''){
			$this->Messager("请将参数都填写完整!", -1);
		}
		$start_time = strtotime($start_time);
		$end_time = strtotime($end_time);
		if ($start_time>=$end_time){
			$this->Messager("开始时间不能等于大于结束时间", -1);
		}
		$data=array(
				'ad_name' => $ad_name,
				'ad_link' => $ad_link,
				'ad_code' => $ad_code,
				'position_id'=>$position_id,
				'city_id'=>$city_id,
				'start_time'=>$start_time,
				'end_time'=>$end_time,
				'enabled'=>$enabled,
					
		);
		$result = dbc(DBCMax)->update('ad')->data($data)->where(array('ad_id' =>$id))->done();
		if (!$result) $this->Messager('添加广告失败！请重试', -1);
		$this->Messager("操作成功",'?mod=ad');
	}
	
	function del(){
		extract($this->Get);
		$result = dbc(DBCMax)->delete('ad')->where('ad_id='.$id)->done();
		if (!$result) $this->Messager('广告不存在!', -1);
		$this->Messager("操作成功",'?mod=ad');
	}
	
	function position(){
		$position_list = logic('ad')->GetPositionList();
		include handler('template')->file('@admin/ad_position_list');
	}
	function position_add(){
		$action='?mod=ad&code=position_doadd';
		include handler('template')->file('@admin/ad_position_add');
	}
	function position_doadd(){
		extract($this->Post);
		if($position_name == ''|| $ad_width == ''||$ad_height==''||$position_style==''){
			$this->Messager("请将参数都填写完整!", -1);
		}
		$data=array(
				'position_name' => $position_name,
				'ad_width' => $ad_width,
				'ad_height' => $ad_height,
				'position_desc'=>$position_desc,
				'position_style'=>$position_style,
		);
		$aid = logic('ad')->add_position($data);
		if (!$aid) $this->Messager('添加广告位失败！请重试', -1);
		$this->Messager("操作成功",'?mod=ad&code=position');
	}
	function position_edit(){
		extract($this->Get);
		$action='?mod=ad&code=position_doedit';
		$p = logic('ad')->getOnePosition($id);
		if(!$p)$this->Messager('参数有误!请重试', '?mod=ad&code=position');
		include handler('template')->file('@admin/ad_position_edit');
	}
	
	function position_doedit(){
		extract($this->Post);
		if($id==""||$position_name == ''|| $ad_width == ''||$ad_height==''||$position_style==''){
			$this->Messager("请将参数都填写完整!", -1);
		}
		$data=array(
				'position_name' => $position_name,
				'ad_width' => $ad_width,
				'ad_height' => $ad_height,
				'position_desc'=>$position_desc,
				'position_style'=>$position_style,
		);

		$result = dbc(DBCMax)->update('ad_position')->data($data)->where(array('position_id' =>$id))->done();
		if (!$result) $this->Messager('添加广告位失败！请重试', -1);
		$this->Messager("操作成功",'?mod=ad&code=position');
	}
	
	function position_del(){
		extract($this->Get);
		$sql='SELECT count(*) as count from '.TABLE_PREFIX.'tttuangou_ad where position_id='.$id;
		$sql = page_moyo($sql);
		$ad = dbc(DBCMax)->query($sql)->done();
		$ad = $ad[0];
		if($ad['count'])$this->Messager('该广告位下有广告,请删除广告后再来删除广告位!', '?mod=ad');
		$result = dbc(DBCMax)->delete('ad_position')->where('position_id='.$id)->done();
		if (!$result) $this->Messager('广告位不存在!', -1);
		$this->Messager("操作成功",'?mod=ad&code=position');
	}
	
	

}

?>